package com.cj.easypoi.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import com.cj.easypoi.entity.Person;
import com.cj.easypoi.util.EasypoiUtil;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * ClassName: FileController <br/>
 * Function: 文件处理 <br/>
 * date: 2018/04/26 15:45 <br/>
 *
 * @author chenj
 * @version 1.0.0
 * @since JDK 1.8
 */
@RestController
@RequestMapping(value = "/file")
public class FileController {

    @PostMapping(value = "/excel/import")
    public void importExcel(@RequestParam("file") MultipartFile file) {
        List<Person> peoples = EasypoiUtil.importExcel(file, 1, 1, Person.class);
        System.out.println(peoples.toString());
    }

    @GetMapping(value = "/excel/export")
    public void exportExcel(HttpServletResponse response) {
        TemplateExportParams templateExcel = new TemplateExportParams("static/easypoi-test.xlsx");
        List<Map<String, String>> listMap = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            Map<String, String> lm = new HashMap<>();
            lm.put("username", "tester" + i);
            lm.put("sex", "男");
            lm.put("birthday", "2018-04-26");
            listMap.add(lm);
        }
        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("list", listMap);

        Workbook workbook = ExcelExportUtil.exportExcel(templateExcel, dataMap);
        EasypoiUtil.downLoadExcel("test.xlsx", response, workbook);
    }
}
